package com.tttshaoqi.server.controller;

import com.tttshaoqi.common.result.Result;
import com.tttshaoqi.common.utils.EmpHolder;
import com.tttshaoqi.pojo.dto.EmployeeDTO;
import com.tttshaoqi.server.mapper.CommentMapper;
import com.tttshaoqi.server.mapper.LikeMapper;
import com.tttshaoqi.server.mapper.PostMapper;
import com.tttshaoqi.pojo.entity.Comment;
import com.tttshaoqi.pojo.entity.Like;
import com.tttshaoqi.pojo.entity.Post;
import com.tttshaoqi.pojo.dto.CommentDTO;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/interact")
@RequiredArgsConstructor
public class InteractionController {

    private final LikeMapper likeMapper;
    private final PostMapper postMapper;
    private final CommentMapper commentMapper;

    @PostMapping("/like/{postId}")
    @Transactional
    public Result<?> like(@PathVariable Long postId){
        EmployeeDTO emp = EmpHolder.getEmp();
        if (emp == null) return Result.error("未登录");
        if (likeMapper.findByUserIdAndPostId(emp.getId(), postId) != null){
            return Result.success();
        }
        Like l = new Like();
        l.setUserId(emp.getId());
        l.setPostId(postId);
        l.setCreatedAt(LocalDateTime.now());
        likeMapper.insert(l);
        Post p = postMapper.findById(postId);
        if (p != null){
            p.setLikeCount(likeMapper.countByPostId(postId));
            postMapper.update(p);
        }
        return Result.success();
    }

    @PostMapping("/unlike/{postId}")
    @Transactional
    public Result<?> unlike(@PathVariable Long postId){
        EmployeeDTO emp = EmpHolder.getEmp();
        if (emp == null) return Result.error("未登录");
        Like exist = likeMapper.findByUserIdAndPostId(emp.getId(), postId);
        if (exist != null){
            likeMapper.delete(exist.getId());
        }
        Post p = postMapper.findById(postId);
        if (p != null){
            p.setLikeCount(likeMapper.countByPostId(postId));
            postMapper.update(p);
        }
        return Result.success();
    }

    @PostMapping("/comment/{postId}")
    public Result<Map<String, Object>> comment(@PathVariable Long postId, @RequestBody CommentDTO req){
        EmployeeDTO emp = EmpHolder.getEmp();
        if (emp == null) return Result.error("未登录");
        Comment c = new Comment();
        c.setPostId(postId);
        c.setUserId(emp.getId());
        c.setContent(req.getContent());
        c.setParentId(req.getParentId());
        c.setCreatedAt(LocalDateTime.now());
        commentMapper.insert(c);
        Post p = postMapper.findById(postId);
        if (p != null){
            p.setCommentCount(commentMapper.getCountByPostId(postId));
            postMapper.update(p);
        }
        return Result.success(Map.of("commentId", c.getId()));
    }

    @GetMapping("/comments/{postId}")
    public Result<List<Comment>> listComments(@PathVariable Long postId){
        // 当前返回 Comment，前端会按 userId 再取用户信息
        return Result.success(commentMapper.listByPostId(postId));
    }

    /**
     * 检查当前用户是否点赞了指定分享
     */
    @GetMapping("/is-liked/{postId}")
    public Result<Map<String, Object>> isLiked(@PathVariable Long postId){
        EmployeeDTO emp = EmpHolder.getEmp();
        if (emp == null) return Result.error("未登录");
        
        Like like = likeMapper.findByUserIdAndPostId(emp.getId(), postId);
        boolean isLiked = like != null;
        
        return Result.success(Map.of("isLiked", isLiked));
    }

    /**
     * 获取用户获赞总数
     */
    @GetMapping("/user/{userId}/liked-count")
    public Result<Map<String, Object>> getUserLikedCount(@PathVariable Long userId){
        Long totalLikedCount = likeMapper.getUserTotalLikedCount(userId);
        return Result.success(Map.of("totalLikedCount", totalLikedCount != null ? totalLikedCount : 0));
    }

    @Data
    public static class CommentReq {
        private String content;
        private Long parentId;
    }
}



